const path = require("path");
const { name } = require("./package");
const webpack = require("webpack");
const ThemeColorReplacer = require("webpack-theme-color-replacer");
const { getThemeColors, modifyVars } = require("./src/utils/themeUtil");
const { resolveCss } = require("./src/utils/theme-color-replacer-extend");
const CompressionWebpackPlugin = require("compression-webpack-plugin");
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
// const TerserPlugin = require("terser-webpack-plugin");
console.log(process.argv[process.argv.length - 1])
let system = ''
try {
	system = process.argv[process.argv.length - 1]
} catch {
	console.log('命令行参数获取错误')
}
console.log(system)
const productionGzipExtensions = ["js", "css"];
const isProd = process.env.NODE_ENV === "production";
// http://localhost:3000/#/fe-qiankun/system/departmentManage
module.exports = {
  // eslint-disable-next-line no-undef
	devServer: {
		open: true,
		port: 9527,
		headers: {
			"Access-Control-Allow-Origin": "*"
		},
		proxy: {
			"/uc/api": {
				//此处要与 /services/api.js 中的 API_PROXY_PREFIX 值保持一致
				target: process.env.VUE_APP_API_BASE_URL,
				changeOrigin: true
			}
			// "/okr_api": {
			// 	//此处要与 /services/api.js 中的 API_PROXY_PREFIX 值保持一致
			// 	target: process.env.VUE_APP_OKR_URL,
			// 	changeOrigin: true,
			// 	pathRewrite: {
			// 		"^/okr_api": "",
			// 	},
			// },
		}
	},
	pluginOptions: {
		"style-resources-loader": {
			preProcessor: "less",
			patterns: [path.resolve(__dirname, "./src/theme/theme.less")]
		}
	},
	configureWebpack: {
		output: {
			library: `${name}-[name]`,
			libraryTarget: "umd", // 把微应用打包成 umd 库格式
			jsonpFunction: `webpackJsonp_${name}`
		},
		performance: {
			hints: false
		},
		plugins: [
			new ThemeColorReplacer({
				fileName: "css/theme-colors-[contenthash:8].css",
				matchColors: getThemeColors(),
				injectCss: true,
				resolveCss
			}),
			new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
			new CompressionWebpackPlugin({
				algorithm: "gzip",
				test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
				threshold: 10240,
				minRatio: 0.8
			})
		]
	},
	chainWebpack: (config) => {
		// 生产环境下关闭css压缩的 colormin 项，因为此项优化与主题色替换功能冲突
		if (isProd) {
			config.plugin("optimize-css").tap((args) => {
				args[0].cssnanoOptions.preset[1].colormin = false;
				return args;
			});
		}
		config.module
			.rule("fonts")
			.test(/.(ttf|otf|eot|woff|woff2)$/)
			.use("url-loader")
			.loader("url-loader")
			.tap((options) => {
				options = {
					// limit: 10000,
					name: "/static/fonts/[name].[ext]"
				};
			return options;
		})
	},
	css: {
		loaderOptions: {
			less: {
				lessOptions: {
					modifyVars: modifyVars(),
					javascriptEnabled: true
				}
			}
		}
	},
	productionSourceMap: false
};